package com.gdk.ksoft.assembly.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by hel on 2014-12-10.
 * 原型进度条
 */
public class CircleProgressBar extends View{

    private long min = 0,max = 100,progress = 0;

    public void setMin(int min){
        if(min>max || min<0) return;
        this.min = min;
        this.invalidate();
    }
    public long getMin(){
        return this.min;
    }
    public void setMax(long max){
        if(max<min) return;
        this.max = max;
        this.invalidate();
    }

    public long getMax(){
        return this.max;
    }

    public void setProgress(long progress){
        if(progress<min || progress>max) return;
        this.progress = progress;
        this.invalidate();
    }

    public long getProgress(){
        return this.progress;
    }

    private Paint paint = new Paint();

    public CircleProgressBar(Context context) {
        super(context);
    }

    public CircleProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.reset();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(20f);
        float percent = (float)progress/max;
        canvas.drawArc(new RectF(10, 10, 130, 130), 270, 360, true, paint);
        paint.setColor(Color.DKGRAY);
        canvas.drawArc(new RectF(10, 10, 130, 130), 270, 360, false, paint);
        paint.setColor(Color.GREEN);
        canvas.drawArc(new RectF(10, 10, 130, 130), 270, percent*360, false, paint);
        paint.setColor(Color.GREEN);
        paint.reset();
        paint.setColor(Color.DKGRAY);
        paint.setAntiAlias(true);
        paint.setTextSize(30f);
        String text = ((int)(percent*100))+"%";
        paint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(text,70,80,paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int wh = MeasureSpec.makeMeasureSpec(150,MeasureSpec.EXACTLY);
        super.onMeasure(wh, wh);
    }
}
